package DTO;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Vector;
import java.util.regex.Matcher;

import BUS.QueryProccessor;

public class DatabaseDTO implements Serializable {
	private String _databaseName;
	private Vector<TableDTO> _tableList;

	public DatabaseDTO(String databaseName) {
		set_databaseName(databaseName);
		set_tableList(new Vector<TableDTO>());
	}

	public void set_databaseName(String _databaseName) {
		this._databaseName = _databaseName;
	}

	public String get_databaseName() {
		return _databaseName;
	}

	public void set_tableList(Vector<TableDTO> _tableList) {
		this._tableList = _tableList;
	}

	public Vector<TableDTO> get_tableList() {
		return _tableList;
	}

	public TableDTO selectQuery(String selectInput, String fromInput,
			String whereInput) throws Exception {
		// TODO Auto-generated method stub

		// select dh.MaDH, dh.MaKH, dh.NgayLap
		// from DON_HANG dh, CT_DONHANG ctdh
		// where ctdh.MaDH = dh.MaDH

		selectInput = selectInput.trim();
		fromInput = fromInput.trim();
		whereInput = whereInput.trim();

		// process FROM
		Vector<String> tableNames = new Vector<String>();
		Vector<String> tableVariables = new Vector<String>();
		String[] temp = fromInput.split(",");
		for (int i = 0; i < temp.length; i++) {
			temp[i] = temp[i].trim();
			String[] tmp = temp[i].split(" ");
			if (tmp.length > 2)
				throw new Exception("Invalid command");
			else {
				tableNames.add(tmp[0]);
				if (tmp.length == 2)
					tableVariables.add(tmp[1]);
			}
		}
		TableDTO table1 = FindTable(tableNames.elementAt(0));
		TableDTO table2 = null;
		TableDTO combineTable = null;
		if (tableNames.size() == 2) {
			table2 = FindTable(tableNames.elementAt(1));
			combineTable = table1.mul(table2);
		} else {
			combineTable = table1;
		}
		TableDTO tableTemp = combineTable.FilterWhere(whereInput, fromInput, table1,
				table2, tableNames, tableVariables);
		TableDTO result = tableTemp.FilterSelect(selectInput, fromInput, table1, table2,
				tableNames, tableVariables);
		return result;
	}

	private TableDTO FindTable(String str) {
		// TODO Auto-generated method stub
		for (int i = 0; i < _tableList.size(); i++) {
			if (_tableList.elementAt(i).get_name().equalsIgnoreCase(str))
				return _tableList.elementAt(i);
		}
		return null;
	}

	public int getColIndex(String str, Vector<String> tableNames,
			Vector<String> tableVariables, String fromInput, Vector<String> originalTableName) {
		// TODO Auto-generated method stub
		str = str.trim();
		int index = str.indexOf(".");
		String tableName = fromInput.trim();
		String colName = str;
		if (index != -1) {
			tableName = str.substring(0, index);
			colName = str.substring(index + 1, str.length());
		}else{
			tableName = fromInput.split(" ")[0];
		}
		
		tableName = FindTableNameIn(tableName, tableNames, tableVariables);
		originalTableName.add(tableName);
		TableDTO table = FindTable(tableName);

		return table.getColIndex(colName);
	}

	private String FindTableNameIn(String tableName, Vector<String> tableNames,
			Vector<String> tableVariables) {
		// TODO Auto-generated method stub
		for (int i = 0; i < tableVariables.size(); i++)
			if (tableVariables.elementAt(i).equalsIgnoreCase(tableName))
				return tableNames.elementAt(i);
		return tableName;
	}
}
